/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package se.bth.ke.firstfriend.dto.todo.helper;

import java.util.Comparator;

/**
 * Comparator which negates the result of a given comparator
 * @author nico.rehwaldt
 */
public class InvertingComparator<T> implements Comparator<T> {

    private Comparator<T> comparator;
    
    private InvertingComparator(Comparator<T> comparator) {
        this.comparator = comparator;
    }
    
    @Override
    public int compare(T o1, T o2) {
        return comparator.compare(o1, o2) * -1;
    }

    public static <T> Comparator<T> inverted(Comparator<T> comparator) {
        if (comparator instanceof InvertingComparator) {
            return ((InvertingComparator<T>) comparator).comparator;
        } else {
            return new InvertingComparator(comparator);
        }
    }
}
